//app.js
const service = require('/service/port.js')
App({
  onLaunch: function () {
    this.globalData.time=1
    this.getAppCode()
    this.computeNavigation()

  },
  globalData: {
    customNavigation: null
  },

  getAppCode(parentId) {
   
    // 登录
    wx.login({
      success: res => {

        wx.setStorageSync("code", res);
        this.getUserInfo(res.code, parentId)
        //发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
  },


  getUserInfo(code, parentId) {
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          var _this = this;
          wx.getUserInfo({
            success: res2 => {
              service.login({
                code: code,
                encryptedData: res2.encryptedData,
                iv: res2.iv,
                id: parentId || 0 
              }, function (result) {
                console.log(result);
                wx.setStorageSync("userInfo", result.data);
                if(result.data.mobile==""){
                  wx.navigateTo({
                    url: 'pages/phone/index',
                  })
                }
                // service.getdiamondNum(result.data.id, res => { //获取钻石数量
                //   wx.setStorageSync("diamondNum", res.data.scort);
                // })
              })
              // 可以将 res 发送给后台解码出 unionId
              _this.globalData.userInfo = res2;
              if (_this.userInfoReadyCallback) {
                _this.userInfoReadyCallback(res2)
              }
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
            }
          })
        }
      }
    })
  },

  computeNavigation() { // 计算导航栏基本信息
    let statusBarHeight = 0;
    let deviceWidth = 0;
    let {
      height,
      top,
      left,
      right,
      width
    } = wx.getMenuButtonBoundingClientRect();
    console.log(wx.getMenuButtonBoundingClientRect());
    let navigationHeight = 0;
    wx.getSystemInfo({
      success: (result) => {
        statusBarHeight = result.statusBarHeight;
        deviceWidth = result.windowWidth;
        navigationHeight = statusBarHeight + height + (top - statusBarHeight) * 2;
        this.globalData.customNavigation = {
          height: navigationHeight,
          top,
          width,
          buttonHeight: height
        }
      },
    });
  },


  globalData: {
    userInfo: null,
    time:0
  }
})